/**   
 * Copyright © 2018 Zuxia. Tech Ltd. All rights reserved.
 * 
 * @Package: com.java46.struts2.base.filter 
 * @author: zuxia   
 * @date: 2018年7月4日 上午10:49:22 
 */
package com.java46.struts2.base.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.java46.struts2.base.http.HttpRequestWrapper;

/** 
 * @ClassName: EncodingFilter 
 * @Description: TODO
 * @author: zuxia
 * @date: 2018年7月4日 上午10:49:22 
 */
@WebFilter(urlPatterns = "/*")
public class EncodingFilter implements Filter {


	public void init(FilterConfig filterConfig) throws ServletException {
	}


	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		// 请求的方法： get|post
		String method = request.getMethod();
		if("GET".equalsIgnoreCase(method)) {
			request = new HttpRequestWrapper(request);
		}else {
			request.setCharacterEncoding("UTF-8");
		}
		response.setCharacterEncoding("UTF-8");
		chain.doFilter(request, response);
	}


	public void destroy() {
	}

}
